package com.lijian.client.controller;

import com.lijian.client.feign.MenuFeign;
import com.lijian.pojo.Menu;
import com.lijian.pojo.MenuResult;
import com.lijian.pojo.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/menu")
public class MenuClientController {

    @Autowired
    private MenuFeign menuFeign;

    @GetMapping
    public String test(){
        return "menu_manage";
    }


    @GetMapping("/findAll")
    @ResponseBody
    public MenuResult findAll(@RequestParam Integer page, @RequestParam Integer limit){
        return menuFeign.findAll(page, limit);
    }

    @GetMapping("/deleteById/{id}")
    public String deleteMenuById(@PathVariable Integer id){
        menuFeign.deleteMenuById(id);
        return "redirect:/menu";
    }

    @GetMapping("/addMenu")
    public String addMenu(Model model){
        model.addAttribute("types", menuFeign.findTypes());
        return "menu_add";
    }

    @PostMapping("/save")
    public String save(Menu menu){
        menuFeign.saveMenu(menu);
        return "redirect:/menu";
    }


    @GetMapping("/updateById/{id}")
    public String update(@PathVariable Integer id, Model model){
        model.addAttribute("types", menuFeign.findTypes());
        Menu menu = menuFeign.findById(id);
        Type type = menuFeign.findTypeById(menu.getTypeId());
        menu.setType(type);
        model.addAttribute("menu", menu);
        return "menu_update";
    }

    @PostMapping("/update")
    public String updateMenu(Menu menu){
        menu.setTypeId(menu.getType().getId());
        menuFeign.update(menu);
        return "redirect:/menu";
    }

}
